import { apiClient } from './index'
import { mockApi } from '@/utils/mockApi'
import type { 
  DatabaseConfig,
  SystemConfig,
  WechatBotConfig,
  ApiResponse,
  PaginationResponse,
  QueryParams 
} from '@/types'

export const systemApi = {
  // 数据库配置管理
  getDatabaseConfigs: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<DatabaseConfig>>> => {
    // 模拟数据
    const mockConfigs: DatabaseConfig[] = [
      {
        id: 1,
        name: 'Redis缓存',
        db_type: 'redis',
        host: 'localhost',
        port: 6379,
        database_name: undefined,
        username: undefined,
        password: undefined,
        connection_pool_size: 20,
        connection_timeout: 30,
        read_timeout: 60,
        write_timeout: 60,
        ssl_enabled: false,
        description: 'Redis缓存数据库配置',
        is_default: true,
        is_active: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z'
      },
      {
        id: 2,
        name: 'StarRocks数据源',
        db_type: 'starrocks',
        host: '192.168.1.100',
        port: 9030,
        database_name: 'default',
        username: 'root',
        password: 'password',
        connection_pool_size: 10,
        connection_timeout: 60,
        read_timeout: 300,
        write_timeout: 300,
        ssl_enabled: false,
        description: 'StarRocks数据源配置',
        is_default: false,
        is_active: true,
        created_at: '2024-01-01T00:00:00Z',
        updated_at: '2024-01-01T00:00:00Z'
      }
    ]
    
    return {
      code: 200,
      message: 'success',
      data: {
        items: mockConfigs,
        pagination: {
          page: 1,
          size: 20,
          total: mockConfigs.length,
          pages: 1
        }
      },
      timestamp: new Date().toISOString()
    }
  },

  createDatabaseConfig: async (data: Partial<DatabaseConfig>): Promise<ApiResponse<DatabaseConfig>> => {
    return {
      code: 200,
      message: 'success',
      data: data as DatabaseConfig,
      timestamp: new Date().toISOString()
    }
  },

  updateDatabaseConfig: async (configId: number, data: Partial<DatabaseConfig>): Promise<ApiResponse<DatabaseConfig>> => {
    return {
      code: 200,
      message: 'success',
      data: data as DatabaseConfig,
      timestamp: new Date().toISOString()
    }
  },

  deleteDatabaseConfig: async (configId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  toggleDatabaseConfig: async (configId: number, isActive: boolean): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  testDatabaseConnection: async (configId: number): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        status: 'success',
        response_time: 15,
        message: '连接成功',
        details: {
          version: '6.0.9',
          memory_usage: '1.2GB',
          connected_clients: 5
        }
      },
      timestamp: new Date().toISOString()
    }
  },

  // 系统配置管理
  getSystemConfigs: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<SystemConfig>>> => {
    return mockApi.system.getConfigs(params)
  },

  updateSystemConfig: async (configKey: string, data: Partial<SystemConfig>): Promise<ApiResponse<SystemConfig>> => {
    return {
      code: 200,
      message: 'success',
      data: data as SystemConfig,
      timestamp: new Date().toISOString()
    }
  },

  // 微信机器人管理
  getWechatBots: async (params?: QueryParams): Promise<ApiResponse<PaginationResponse<WechatBotConfig>>> => {
    return mockApi.system.getWechatBots(params)
  },

  createWechatBot: async (data: Partial<WechatBotConfig>): Promise<ApiResponse<WechatBotConfig>> => {
    return {
      code: 200,
      message: 'success',
      data: data as WechatBotConfig,
      timestamp: new Date().toISOString()
    }
  },

  updateWechatBot: async (botId: number, data: Partial<WechatBotConfig>): Promise<ApiResponse<WechatBotConfig>> => {
    return {
      code: 200,
      message: 'success',
      data: data as WechatBotConfig,
      timestamp: new Date().toISOString()
    }
  },

  deleteWechatBot: async (botId: number): Promise<ApiResponse<void>> => {
    return {
      code: 200,
      message: 'success',
      data: undefined,
      timestamp: new Date().toISOString()
    }
  },

  testWechatBot: async (botId: number, message: string): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        status: 'success',
        message: '消息发送成功'
      },
      timestamp: new Date().toISOString()
    }
  },

  // 系统状态
  getSystemStatus: async (): Promise<ApiResponse<any>> => {
    return {
      code: 200,
      message: 'success',
      data: {
        system: {
          version: '2.0.0',
          uptime: 86400,
          status: 'healthy'
        },
        database: {
          mysql: 'connected',
          starrocks: 'connected',
          redis: 'connected'
        },
        services: {
          flink_api: 'available',
          celery: 'running',
          monitoring: 'active'
        }
      },
      timestamp: new Date().toISOString()
    }
  }
} 